﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using System.IO;
using Microsoft.SharePoint;
using System.Security;

namespace IronSharePoint
{
    public class IronScriptResolver : IIronScriptResolver
    {
        private Guid _featureId;

        public Guid FeatureId 
        {
            get { return _featureId; }
            set
            {
                _featureId = value;
                
                if(_featureId!=Guid.Empty)
                    SearchPaths.Add(Path.Combine(SPFarm.Local.FeatureDefinitions[_featureId].RootDirectory, "Scripts"));
            }
        }
        
        public List<string> SearchPaths { get; set; }

        public string ScriptPath { get; set; }

        public IronScriptResolver()
        {
            SearchPaths = new List<string>();

            SearchPaths.Add(Path.Combine(SPFarm.Local.FeatureDefinitions[IronConstant.WebAppFeatureId].RootDirectory + "\\..\\..\\", "IronSharePoint.Modules"));
            SearchPaths.Add(Path.Combine(SPFarm.Local.FeatureDefinitions[IronConstant.WebAppFeatureId].RootDirectory + "\\..\\..\\", "IronSharePoint.Modules\\Lib"));
        }

        public string GetScript(string scriptPath)
        {
            string script = null;

            if (scriptPath.StartsWith("http://") || scriptPath.StartsWith("https://"))
            {
                IronSettings store = IronSettings.Current;

                foreach (string trustedLocation in store.TrustedLocations)
                {
                    if (scriptPath.StartsWith(trustedLocation.Replace("*", "")))
                    {
                        using(SPSite site = new SPSite(scriptPath))
                        {
                            using (SPWeb web = site.OpenWeb())
                            {
                                script = web.GetFileAsString(scriptPath);
                                if (String.IsNullOrEmpty(script))
                                    throw new FileNotFoundException("Script not found!");
                                
                                ScriptPath = scriptPath;

                                return script;
                            }
                        }
                    }
                }

                throw new SecurityException("Not a trusted script location!");
            }

            if(scriptPath.ToUpper().Contains("\\template\\layouts\\".ToUpper())
                || scriptPath.ToUpper().Contains("\\template\\controltemplates\\".ToUpper())
                || scriptPath.ToUpper().Contains("\\template\\isapi\\".ToUpper())
                || scriptPath.ToUpper().Contains("\\template\\admin\\".ToUpper()))
            {
                if (scriptPath.Contains("/py/"))
                {
                    scriptPath = scriptPath + ".py";
                }
                else if(scriptPath.Contains("/rb/"))
                {
                    scriptPath += scriptPath + ".rb";
                }
                else if (File.Exists(scriptPath + ".py"))
                {
                    scriptPath = scriptPath + ".py";
                }
                else if (File.Exists(scriptPath + ".rb"))
                {
                    scriptPath = scriptPath + ".rb";
                }

                this.ScriptPath = scriptPath;

                script = File.ReadAllText(scriptPath);

                return script;
            }

            foreach (string searchPath in SearchPaths)
            {
                string filePath = Path.Combine(searchPath, scriptPath);
                if (File.Exists(filePath))
                {
                    script = File.ReadAllText(Path.Combine(searchPath, scriptPath));
                    this.ScriptPath = Path.Combine(searchPath, scriptPath);
                    break;
                }
            }

            

            if(script==null)
                throw new FileNotFoundException(scriptPath + " not found!");

            return script;
        }

      
    }
}
